#include "scrambled_symbols.h"
#include "gisusswfile.hxx"
#include "gisusswfileset.hxx"

#include "priv/dbcache.hxx"

DBCache::DBCache()
  : _softwareid(),_softwareid_protect(),
    _swfiles(),_swfiles_protect(),
    _chunkfile(),_chunkfile_protect()
{
}

DBCache::~DBCache()
{
}

bool DBCache::GetSoftwareId(const string &model, const string &com_ref, string &softwareid) const
{
  bool res;
  tSoftwareIdCacheContainer::const_iterator it;
  _softwareid_protect.Lock();

  it=_softwareid.find(make_pair(model,com_ref));

  if (it==_softwareid.end())
    {
      softwareid.clear();
      res=false;
    }
  else
    {
      res=true;
      softwareid=(*it).second;
    }

  _softwareid_protect.Unlock();
  
  return res;
}

void DBCache::SetSoftwareId(const string &model, const string &com_ref, const string &softwareid)
{
  _softwareid_protect.Lock();
  _softwareid.insert(make_pair(make_pair(model,com_ref),softwareid));
  _softwareid_protect.Unlock();
}

bool DBCache::GetSWFiles(const string &model, const string &comref, const string &softwareid, GisusSWFileSet *gisusswfileset) const
{
  bool res;
  tSWFileCacheContainer::const_iterator it;

  _swfiles_protect.Lock();

  it=_swfiles.find(make_pair(make_pair(model,comref),softwareid));

  if (it==_swfiles.end())
    {
      res=false;
    }
  else
    {
      res=true;
      *gisusswfileset=(*it).second;
    }

  _swfiles_protect.Unlock();

  return res;
}

void DBCache::SetSWFiles(const string &model, const string &comref, const string &softwareid, GisusSWFileSet *gisusswfileset)
{
  _swfiles_protect.Lock();
  _swfiles.insert(make_pair(make_pair(make_pair(model,comref),softwareid),*gisusswfileset));
  _swfiles_protect.Unlock();
}

bool DBCache::GetChunkFileName(tUInt32 fileid,tUInt16 chunkid, string &chunkfilename) const
{
  bool res;
  tChunkFileCacheContainer::const_iterator it;

  _chunkfile_protect.Lock();

  it=_chunkfile.find(make_pair(fileid,chunkid));

  if (it==_chunkfile.end())
    {
      chunkfilename.clear();
      res=false;
    }
  else
    {
      res=true;
      chunkfilename=(*it).second;
    }

  _chunkfile_protect.Unlock();

  return res;
}

void DBCache::SetChunkFileName(tUInt32 fileid,tUInt16 chunkid, const string &chunkfilename)
{
  _chunkfile_protect.Lock();
  _chunkfile.insert(make_pair(make_pair(fileid,chunkid),chunkfilename));
  _chunkfile_protect.Unlock();
}
